<template>
  <div class="resource-category">
    <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-button @click="handleAdd">添加</el-button>
      <el-button style="float: right; padding: 3px 0" type="text" @click="$router.back()">返回</el-button>
    </div>
    <el-table
      v-loading="loading"
      :data="categoryList"
      border
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="name"
        label="名称"
        align="center"
        min-width="150">
      </el-table-column>
      <el-table-column
        prop="createdTime"
        label="创建时间"
        align="center"
        min-width="150">
        <template slot-scope="scope">
          <span>{{scope.row.createdTime | formatDate}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
        align="center"
        min-width="150">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        min-width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <el-dialog :title="handleTitle" :visible.sync="visible" width="30%">
      <el-form :model="handleForm">
        <el-form-item label="名称" prop="name" :label-width="formLabelWidth">
          <el-input v-model="handleForm.name" autocomplete="off" :style="inputWidth"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort" :label-width="formLabelWidth">
          <el-input v-model="handleForm.sort" autocomplete="off" :style="inputWidth"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" :disabled="loading"  @click="saveOrUpdateResource">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import {
  getAllCategory,
  categorySaveOrUpdate,
  deleteResourceCategory
} from '@/services/resource'

export default Vue.extend({
  name: 'ResourceCategory',
  data () {
    return {
      isEdit: false,
      formLabelWidth: '120px',
      categoryList: [],
      inputWidth: {
        width: '250px'
      },
      handleForm: {
        name: '',
        sort: 0
      },
      loading: false,
      visible: false
    }
  },
  computed: {
    handleTitle () {
      return (this as any).isEdit ? '编辑分类' : '添加分类'
    }
  },
  created () {
    this.loadAll()
  },
  methods: {
    async loadAll () {
      this.loading = true
      const { data } = await getAllCategory()
      this.loading = false
      this.categoryList = data.data
    },
    async handleAdd () {
      this.isEdit = false
      this.handleForm = {
        name: '',
        sort: 0
      }
      this.visible = true
    },
    async handleEdit (row: any) {
      this.handleForm = row
      this.isEdit = true
      this.visible = true
    },
    async handleDelete (row: any) {
      this.$confirm('是否删除该资源?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteResourceCategory(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(console.log)
    },
    async saveOrUpdateResource () {
      this.loading = true
      await categorySaveOrUpdate(this.handleForm)
      this.loading = false
      this.visible = false
      this.$message.success(`${this.isEdit ? '修改成功' : '添加成功'}`)
      this.loadAll()
    }
  }
})
</script>

<style>

</style>
